﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AppMachine.Controls.ValidationRules {
    public class ValidationRuleRegularExpression : ValidationRule {
        public string RegularExpression { get; set; }
        /// <summary>
        /// 触发时错误提示信息
        /// </summary>
        public string ErrorContent { get;set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            if (value == null || !new Regex(this.RegularExpression).IsMatch(value.ToString())) {
                string tempErrorContent = $"错误输入，必须满足正则{RegularExpression}";
                if (!string.IsNullOrWhiteSpace(this.ErrorContent)) {
                    tempErrorContent = this.ErrorContent;
                }
                return new ValidationResult(false, tempErrorContent);
            }

            return ValidationResult.ValidResult;
        }
    }
}
